package com.biostime.qdingding.modules.qd.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.service.CrudService;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.modules.qd.dao.PromotionsetDao;
import com.biostime.qdingding.modules.qd.entity.Contract;
import com.biostime.qdingding.modules.qd.entity.Promotionset;

/**
 * 促销Service
 * @author king
 * @version 2015-12-09
 */
@Service
@Transactional(readOnly = true)
public class PromotionsetService extends CrudService<PromotionsetDao, Promotionset> {
	@Override
	public List<Promotionset> findList(Promotionset entity) {
		// TODO Auto-generated method stub
		dataScopeFilter(entity, "dsf", "id=a.center_id", "id=a.create_by");
		
		return super.findList(entity);
	}
	
	public List<Map<String, String>> getDetail(Integer id){
		return dao.getDetail(id);
	}
	
	public List<Map<String,String>> getHint(String setId){
		return dao.getHint(setId);
	}
	
	public String getPromHint(String setId){
		List<String> proms = new ArrayList<String>();
		List<Map<String, String>> list = this.getHint(setId);
		for(Map<String,String> map : list){
			Set<String> set = map.keySet();
			String type = String.valueOf(map.get("type"));
			for (Iterator iterator = set.iterator(); iterator.hasNext();) {
				String key = (String) iterator.next();
				switch (key) {
				case "setName":
					String setName = map.get(key);
					if(proms.isEmpty()){
						proms.add(setName);
						break;
					}
					for(int i = 0 ; i <proms.size(); i++){
						String temp = proms.get(i);
						if(!StringUtils.isEmpty(temp) && setName.equalsIgnoreCase(temp)){
							break;
						}
						if((i+1) == proms.size()){
							proms.add(setName);
							break;
						}
					}
					break;
				case "promName":
					String promName = map.get(key);
					proms.add("&nbsp;&nbsp;"+promName+":");
					break;
				case "json":
					String json = map.get(key);
					if(type.equalsIgnoreCase("1")){
						JSONArray jsonArray = new JSONArray(json);
						StringBuffer buffer = new StringBuffer();
						for (int i = 0; i < jsonArray.length(); i++) {   
							JSONObject jsonobj = jsonArray.getJSONObject(i);   
							String level = jsonobj.getString("level");
							String levelName = jsonobj.getString("levelName");
							Integer discount = jsonobj.getInt("discount");
							buffer.append("&nbsp;&nbsp;&nbsp;"+level+"元,"+levelName+discount+"折;");
					    }   
						proms.add(buffer.toString());
					}else if(type.equalsIgnoreCase("2")){
						JSONObject jsonObject = new JSONObject(json);
						int n = jsonObject.getInt("n");
						int money =  jsonObject.getInt("money");
						proms.add("&nbsp;&nbsp;&nbsp;前"+n+"位预约成功的用户返还"+money+"元");
					}
					break;
				default:
					break;
				}
			}
		}
		StringBuffer buffer = new StringBuffer();
		for(String str : proms){
			buffer.append(str+"<br>");
		}
		return buffer.toString();
	}
	
	public List<String> getProms(Contract contract){
		List<String> proms = new ArrayList<String>();
		List<Map<String, String>> list = this.getDetail(Integer.valueOf(contract.getStudent().getId()));
		for(Map<String,String> map : list){
			Set<String> set = map.keySet();
			String type = String.valueOf(map.get("type"));
			for (Iterator iterator = set.iterator(); iterator.hasNext();) {
				String key = (String) iterator.next();
				switch (key) {
				case "setName":
					String setName = map.get(key);
					if(proms.isEmpty()){
						proms.add(setName);
						break;
					}
					for(int i = 0 ; i <proms.size(); i++){
						String temp = proms.get(i);
						if(!StringUtils.isEmpty(temp) && setName.equalsIgnoreCase(temp)){
							break;
						}
						if((i+1) == proms.size()){
							proms.add(setName);
							break;
						}
					}
					break;
				case "promName":
					String promName = map.get(key);
					proms.add("&nbsp;&nbsp;"+promName+":");
					break;
				case "json":
					String json = map.get(key);
					if(type.equalsIgnoreCase("1")){
						JSONArray jsonArray = new JSONArray(json);
						StringBuffer buffer = new StringBuffer();
						for (int i = 0; i < jsonArray.length(); i++) {   
							JSONObject jsonobj = jsonArray.getJSONObject(i);   
							String level = jsonobj.getString("level");
							String levelName = jsonobj.getString("levelName");
							Double discount = jsonobj.getDouble("discount");
							buffer.append("&nbsp;&nbsp;&nbsp;"+level+"元,"+levelName+discount+"折;");
					    }   
						proms.add(buffer.toString());
					}else if(type.equalsIgnoreCase("2")){
						JSONObject jsonObject = new JSONObject(json);
						int n = jsonObject.getInt("n");
						int money =  jsonObject.getInt("money");
						proms.add("&nbsp;&nbsp;&nbsp;前"+n+"位预约成功的用户返还"+money+"元");
					}
					break;
				default:
					break;
				}
			}
		}
		return proms;
	}
}
